package com.medicine.hbp.ui;

import java.io.File;

import org.kymjs.kjframe.KJBitmap;
import org.kymjs.kjframe.http.HttpParams;
import org.kymjs.kjframe.ui.BindView;
import org.kymjs.kjframe.widget.RoundImageView;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.medicine.hbp.base.BaseActivity;
import com.medicine.hbp.dialog.PhotographDialog;
import com.medicine.hbp.dialog.PhotographDialog.PhotographDialogListener;
import com.medicine.hbp.http.AppHttpCallBack;
import com.medicine.hbp.http.AppHttps;
import com.medicine.hbp.http.HttpInterface;
import com.medicine.hbp.http.UploadUtil;
import com.medicine.hbp.http.UploadUtil.ImageCallBack;
import com.medicine.hbp.sp.LoginSp;
import com.medicine.hbp.uitl.ChoosePicturesUtils;
import com.medicine.hbp.uitl.CommUtil;
import com.medicine.hbp.uitl.ToastUtil;
import com.medicine.hbp.R;

public class UserSettingActivity extends BaseActivity {
	@BindView(id = R.id.my_nickname_layout, click = true)
	public RelativeLayout my_nickname_layout;// 昵称
	@BindView(id = R.id.my_sign_layout, click = true)
	public RelativeLayout my_sign_layout;// 签名
	@BindView(id = R.id.my_gender_layout, click = true)
	public RelativeLayout my_gender_layout;// 性别
	@BindView(id = R.id.my_addr_layout, click = true)
	public RelativeLayout my_addr_layout;// 地址
	@BindView(id = R.id.my_avatar_layout, click = true)
	public RelativeLayout my_avatar_layout;

	@BindView(id = R.id.start_psd_back, click = true)
	public RelativeLayout start_psd_back;

	@BindView(id = R.id.txt_nickname)
	public TextView txt_nickname;// 昵称
	@BindView(id = R.id.txt_sign)
	public TextView txt_sign;// 签名
	@BindView(id = R.id.txt_gender)
	public TextView txt_gender;// 性别
	@BindView(id = R.id.txt_addr)
	public TextView txt_addr;// 地址
	@BindView(id = R.id.txt_phone)
	public TextView txt_phone;// 电话

	@BindView(id = R.id.head_imageview)
	public RoundImageView round_imageview;//用户头像
	private String  sex="";//Y 男 N 女
	private String  url="";
	private String  Provice="";
	private String  City="";
	private String  District="";
	private KJBitmap bitmap=new KJBitmap();
	@Override
	public void setRootView() {
		super.setRootView();
		setContentView(R.layout.activity_my_profile_setting);
	}

	@Override
	public void initWidget() {
		super.initWidget();
		txt_nickname.setText(LoginSp.getUserName(aty));
		if (TextUtils.isEmpty(LoginSp.getUserSignature(aty))) {
			txt_sign.setText("暂无");
		} else {
			txt_sign.setText(LoginSp.getUserSignature(aty));
		}
		sex = LoginSp.getSex(aty);
		txt_addr.setText(LoginSp.getUserArea(aty));
		url=LoginSp.getUserHead(aty);
		bitmap.display(round_imageview, LoginSp.getUserHead(aty),50,50);
		if(sex.equals("Y")){
			txt_gender.setText("男");
		}else if(sex.equals("N")){
			txt_gender.setText("女");
		}
		initTop();
	}

	@Override
	public void widgetClick(View v) {
		super.widgetClick(v);
		Intent intent = null;
		switch (v.getId()) {
			case R.id.my_avatar_layout:// 头像
				startPhotographDialog();
				break;
			case R.id.start_psd_back:// 密码修改
				intent = new Intent(aty, PasswordBackActivity.class);
				startActivity(intent);
				break;
			case R.id.my_nickname_layout:// 昵称
				intent = new Intent(aty, UserSettingSelectActivity.class);
				intent.putExtra("type", 1);
				startActivityForResult(intent, 111);
				break;
			case R.id.my_sign_layout:// 签名
				intent = new Intent(aty, UserSettingSelectActivity.class);
				intent.putExtra("type", 2);
				startActivityForResult(intent, 222);
				break;
			case R.id.my_gender_layout:// 性别
				intent = new Intent(aty, UserSettingSelectActivity.class);
				intent.putExtra("type", 3);
				startActivityForResult(intent, 333);
				break;
			case R.id.my_addr_layout:// 地址
				intent = new Intent(aty, UserSettingSelectActivity.class);
				intent.putExtra("type", 4);
				startActivityForResult(intent, 444);
				break;
			default:
				break;
		}
	}
	/**
	 * 调用相机拍照选择
	 */
	private void startPhotographDialog() {
		PhotographDialog photographDialog = new PhotographDialog(aty,
				new PhotographDialogListener() {
					@Override
					public void onSelect(int position) {
						if (position == 0) {
							ChoosePicturesUtils.photographic(aty);
						} else {
							ChoosePicturesUtils.pickPhonoImage(aty);
						}
					}
					@Override
					public void onCancel() {
					}
				}, 0);
		photographDialog.showPhotographDialog();
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (resultCode) { // resultCode为回传的标记，我在B中回传的是RESULT_OK
			case RESULT_OK:
				if(requestCode==111){
					Bundle b = data.getExtras();
					String str = b.getString("str");// str即为回传的值
					txt_nickname.setText(str);
				}else if(requestCode==222){
					Bundle b = data.getExtras();
					String str = b.getString("str");// str即为回传的值
					txt_sign.setText(str);
				}else if(requestCode==333){
					Bundle b = data.getExtras();
					sex = b.getString("str");// str即为回传的值
					if(sex.equals("Y")){
						txt_gender.setText("男");
					}else if(sex.equals("N")){
						txt_gender.setText("女");
					}
				}else if(requestCode==444){
					Bundle b = data.getExtras();
					Provice = b.getString("Provice");//省
					City = b.getString("City");// 市
					District = b.getString("District");// 区
					txt_addr.setText(Provice+" "+City+" "+District);
				}

				// 存储卡可用
				if (ChoosePicturesUtils.isHasSdcard()) {
					if (requestCode == ChoosePicturesUtils.REQUEST_CODE_TAKE_PICTURE) {// 拍照返回
						if (!TextUtils.isEmpty(ChoosePicturesUtils.imagePathString)) {
//									imageThrred(ChoosePicturesUtils.imagePathString);
							ChoosePicturesUtils.cropPictures(aty,Uri.fromFile(new File(ChoosePicturesUtils.imagePathString)));
						}
					} else if (ChoosePicturesUtils.PHOTOZOOM == requestCode) {
						try {
							Uri uri = data.getData();
							if (uri != null) {
								String path = CommUtil.getImageAbsolutePath(aty,
										uri);
								if (!TextUtils.isEmpty(path)) {
									ChoosePicturesUtils.cropPictures(aty,Uri.fromFile(new File(path)));
//											imageThrred(path);
								} else {
									ToastUtil.showShort(aty, "从相册中选取照片失败");
								}
							}
						} catch (Exception e) {
							Log.e("ReleaseActivity", e.getMessage());
							e.printStackTrace();
						} finally {
						}
					}
					else  if (ChoosePicturesUtils.REQUEST_CROP_ICON == requestCode) {
						if (!TextUtils.isEmpty(ChoosePicturesUtils.imagePathString)) {
							imageThrred(ChoosePicturesUtils.imagePathString);
						}
					}
				}
				break;
			default:
				break;
		}

	}
	private void imageThrred(final String path) {
		new Thread(new Runnable() {
			@Override
			public void run() {
//				httpFile(CommUtil.qrcodeMyBitmap(CommUtil.getimage(path), aty));
				httpFile(path);
			}
		}).start();
	}

	/**
	 * 文件上传
	 * @param file
	 */
	private void httpFile(final String file) {
		UploadUtil.uploadFile(new File(file), AppHttps.HTTP_URL+HttpInterface.UPLOAD_FILE,new ImageCallBack() {
			@Override
			public void onSuccess(String urls) {
				url=urls;
				bitmap.display(round_imageview, url,50,50);
			}
			@Override
			public void onFailure(String error) {
				ToastUtil.showShort(aty, error);
			}
		});

	}
	private void initTop() {
		TopView topview = (TopView) findViewById(R.id.topview);
		topview.setTitle("信息设置");
		topview.setLeftVisibility(View.GONE);
		topview.setLeftVisibilityExit(View.VISIBLE);
		topview.setRightTxtVisibility(View.VISIBLE);
		topview.setRightTitle("提交");
		topview.setRightVisibility(View.INVISIBLE);
		topview.setTextRightOnClick(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				http();
			}
		});
		topview.setLeftOnClickExit(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
	}

	/**
	 * 用户信息更新接口
	 */
	private void http() {
		HttpParams params = new HttpParams();
		params.put("userHeadImg", url);// 用户头像
		params.put("userSignature", txt_sign.getText().toString());// 用户签名
		params.put("username", txt_nickname.getText().toString());// 用户昵称
		params.put("userSex", sex);// 性别
		params.put("province", Provice);// 省份
		params.put("city", City);// 城市
		params.put("county", District);// 区县
		params.put("area", Provice+City+District);// 详细地址

		AppHttps.HttpPost(this, HttpInterface.UPDATE_USER, params,
				new AppHttpCallBack<Object>(Object.class, aty) {
					@Override
					public void onSuccess(Object result) {
						LoginSp.setUserHead(url, aty);
						LoginSp.setUserSignature( txt_sign.getText().toString(), aty);
						LoginSp.setUserName(txt_nickname.getText().toString(), aty);
						LoginSp.setSex(sex, aty);
						LoginSp.setProvice(Provice, aty);
						LoginSp.setProvice(City, aty);
						LoginSp.setProvice(District, aty);
						LoginSp.setUserArea(Provice+City+District, aty);
						finish();
					}

					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(aty, errorMsg);
					}
				});

	}
}
